package com.lovo.controller;

import com.google.gson.Gson;
import com.lovo.bean.RoleThrone;
import com.lovo.service.RoleThroneService;
import com.lovo.service.serviceImpl.RoleThroneServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "RoleThroneServlet",urlPatterns = "/roleThrone.do")
public class RoleThroneServlet extends HttpServlet {
    RoleThroneService rts =new RoleThroneServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("html/text;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){

        }else if(method.equals("getThroneByRoleId")){
            getThroneByRoleId(request,response);
        }
    }

    private void getThroneByRoleId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("roleId");
        int roleId =0;
        if(id!=null){
            roleId =Integer.parseInt(id);
        }
        List<RoleThrone> roleThrones = rts.selectByRoleId(roleId);
        String json = new Gson().toJson(roleThrones);
        response.getWriter().write(json);
    }
}
